import { resolve } from 'node:path'

import vue from '@vitejs/plugin-vue'
import vueJSX from '@vitejs/plugin-vue-jsx'
// import taichi from 'rollup-plugin-taichi'
import { defineConfig } from 'vite'
import { chunkSplitPlugin } from 'vite-plugin-chunk-split'
// import vueDevTools from 'vite-plugin-vue-devtools'
import zipPack from 'vite-plugin-zip-pack'

function getZipFileName(mode: string): string {
  return `${mode}_${new Date()
    .toLocaleString()
    .replace(/\/|:/g, '')
    .replace(/\s/g, '_')}.zip`
}

// https://vitejs.dev/config/
export default ({ mode }) => {
  return defineConfig({
    plugins: [
      vue(),
      chunkSplitPlugin({
        strategy: 'default',
      }),
      vueJSX(),
      zipPack({
        inDir: `dist/${mode}`,
        outDir: `dist/${mode}`,
        outFileName: getZipFileName(mode),
      }),
      // vueDevTools(),
      // taichi(),
    ],
    base: './',
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'), // 设置 `@` 指向 `src` 目录
      },
    },
    server: {
      host: '0.0.0.0',
      port: 8080, // 设置服务启动端口号
      open: true, // 设置服务启动时是否自动打开浏览器
      // 代理
      proxy: {},
    },
    css: {
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler',
          additionalData: '@use "/src/styles/variables.scss" as *;',
        },
      },
    },
    build: {
      outDir: `dist/${mode}`,
      minify: 'terser', // boolean | 'terser' | 'esbuild'
      // 生产环境移除 console
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
      chunkSizeWarningLimit: 500,
      rollupOptions: {
        // 打包文件拆分
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
        },
      },
    },
  })
}
